//
//  FAQMainViewController.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/11.
//

#import "FAQMainViewController.h"
#import "JXCategoryView/JXCategoryView.h"
#import "FAQSourchView.h"
#import "DKFaqSourchController.h"
@interface FAQMainViewController ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong)FAQSourchView * faqSourchView;
@property (nonatomic,strong) NSArray *faqTitles;
@property (nonatomic, copy)NSString * sourchContent;
@end
@implementation FAQMainViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self dk_addSubView];
    [self makeNavBack];
    [self creatContainerView];
    [self requestFaqTitle];
    [DKPublicHttp apiUserBrhavRecordForAPI:@{} withPageName:DK_FAQ withAction:DK_ActionEnter complete:^{
            
    } fail:^{
            
    }];
}
- (void)dk_addSubView{
    WeakSelf(self)
    UIImageView* dk_FaqBgImgView = [[UIImageView alloc] init];
    dk_FaqBgImgView.userInteractionEnabled = YES;
    dk_FaqBgImgView.image = DK_GetImage(@"dk_MeFaqBg");
    dk_FaqBgImgView.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:dk_FaqBgImgView];
    [dk_FaqBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(DK_IsWidthScale(0)));
    }];
    
    UILabel * dk_FaqTitleLab = [[UILabel alloc] init];
    dk_FaqTitleLab.textColor = DK_Hex(@"#333333");
    dk_FaqTitleLab.font = DK_FONT_Medium(17);
    dk_FaqTitleLab.text = @"FAQ";
    [self.view addSubview:dk_FaqTitleLab];
    [dk_FaqTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(12)+DK_SafeAreaTop));
        make.left.equalTo(@(DK_IsWidthScale(61)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
    }];
    self.faqSourchView = [[FAQSourchView alloc] init];
    [self.view addSubview:self.faqSourchView ];
    [self.faqSourchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(80)+DK_SafeAreaTop));
        make.left.equalTo(@(DK_IsWidthScale(23)));
        make.right.equalTo(@(-DK_IsWidthScale(23)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.faqSourchView.FaqSourchBlock = ^(NSString * _Nonnull sourchContent) {
        weakSelf.sourchContent = sourchContent;
        [weakSelf.listContainerView reloadData];
    };
    UIButton * dk_faqAgreementBtn = [[UIButton alloc] init];
    [dk_faqAgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [dk_faqAgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    dk_faqAgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [dk_faqAgreementBtn addTarget:self action:@selector(CheckAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:dk_faqAgreementBtn];
    [dk_faqAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(@(-DK_IsWidthScale(30)-DK_SafeAreaBottom));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
}
- (void)creatContainerView{
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    self.listContainerView.scrollView.scrollEnabled = NO;
    [self.view addSubview:self.listContainerView];
    [self.listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(198)+DK_SafeAreaTop));
        make.bottom.equalTo(@(-DK_IsWidthScale(137)-DK_SafeAreaBottom));
    }];
    
    self.categoryView = [[JXCategoryTitleView alloc] init];
//                         WithFrame:CGRectMake(0, DK_IsWidthScale(150)+DK_SafeAreaTop, DK_SCREEN_W, DK_IsWidthScale(28))];
    self.categoryView.delegate = self;
    self.categoryView.averageCellSpacingEnabled = NO;
    self.categoryView.cellWidthZoomScrollGradientEnabled = YES;
    self.categoryView.cellWidthZoomEnabled = YES;
    self.categoryView.backgroundColor = UIColor.clearColor;
    self.categoryView.titleFont = DK_FONT_Regular(14);
    self.categoryView.titleSelectedFont = DK_FONT_Regular(14);
    self.categoryView.titleColor = DK_Hex(@"#999999");
    self.categoryView.titleSelectedColor = DK_Hex(@"#000000");
    self.categoryView.defaultSelectedIndex = 1;
    [self.view addSubview:self.categoryView];
    [self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(self.faqSourchView .mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(28)));
    }];
    // 下划线
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = DK_Hex(@"#00CBFF");
    lineView.indicatorWidth = DK_IsWidthScale(50);
    lineView.indicatorHeight = DK_IsWidthScale(4);
    _categoryView.indicators = @[lineView];
    self.categoryView.listContainer = self.listContainerView;
}
- (void)requestFaqTitle{
    WeakSelf(self)
    [DKPublicHttp apiFaqGetTypeListForAPI:@{} complete:^(NSArray * _Nonnull faqTitleMdls) {
        weakSelf.faqTitles = faqTitleMdls;
        [weakSelf makeFaqTitle];
    }];
}
- (void)makeFaqTitle{
    NSMutableArray *faqTitles = [[NSMutableArray alloc] init];
    for (DKFaqTitleModel *faqTitleMdl in self.faqTitles) {
        [faqTitles addObject:faqTitleMdl.type];
    }
    self.categoryView.titles = faqTitles;
    self.categoryView.defaultSelectedIndex = 0;
    [self.categoryView reloadData];
}
- (void)CheckAgreement{
    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:DK_PrivacyAgreement withIsNavBar:YES];
//    dk_WebViewCtrl.webUrl = DK_PrivacyAgreement;
    [self.navigationController pushViewController:dk_WebViewCtrl animated:YES];
}
#pragma mark -- JXCategoryViewDelegate
// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index{
   
}
// 点击选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index{
    
}
// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index{
    
}
// 正在滚动中的回调
- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio{
    
}
#pragma mark -- JXCategoryListContainerViewDelegate
// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.faqTitles.count;
}
// 根据下标 index 返回对应遵守并实现 `JXCategoryListContentViewDelegate` 协议的列表实例
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    WeakSelf(self)
    DKFaqSourchController *faqSourchCtrl = [[DKFaqSourchController alloc] init];
    faqSourchCtrl.sourchContent = self.sourchContent;
    faqSourchCtrl.faqTitleMdl = self.faqTitles[index];
    faqSourchCtrl.UpdataDataBlock = ^{
        [weakSelf requestFaqTitle];
    };
    return faqSourchCtrl;
}
@end
